const state = {
  carShopList: [],
  orderList: [],
}

const mutations = {
  // 存储订单列表
  REQORDERLIST(state, value) {
    state.orderList = value
  },
}

const actions = {
  // 根据省份和城市获取有这个类型并且没被租借的车
  async reqOrder({ commit }, payload) {
    const ret = await reqUserOrder(payload)
    if (ret.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error(ret.msg))
    }
  },
  // 获取该用户的订单记录
  async reqOrderList({ commit }) {
    const ret = await reqUserOrderList()
    if (ret.code == 200) {
      commit('REQORDERLIST', ret.data)
    }
  },
}

//计算属性
const getters = {
  userOrderList(state) {
    return state.orderList.map(order => {
      let data = { ...order, ...order.car, ...order.user, ...order.shop }
      delete data.car
      delete data.user
      delete data.shop
      return data
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
}
